//
//  UploadRequest.m
//  微他
//
//  Created by 微他 on 14-7-21.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "UploadRequest.h"
#import "AFHTTPRequestOperationManager.h"

@implementation UploadRequest

+ (void)startRequest:(NSString *)url image:(UIImage *)image parameters:(NSDictionary *)parameters imageAddress:(NSString *)imageAddress com1:(void(^)(id sta1))_com1 com2:(void(^)(id sta2))_com2
{
    NSMutableDictionary *paramsDics = [[NSMutableDictionary alloc] init];
    [paramsDics setObject:@1 forKey:@"mob"];
    [paramsDics setObject:@"2.1.1" forKey:@"appversion"];
    NSString *appid = [[NSUserDefaults standardUserDefaults] objectForKey:@"appid"];
    NSString *lastTime = [[NSUserDefaults standardUserDefaults] objectForKey:@"applasttime"];
    //检查是否存在appid
    if (appid != nil && lastTime !=nil) {
        [paramsDics setObject:appid forKey:@"appid"];
        [paramsDics setObject:lastTime forKey:@"applasttime"];
    }else
    {
        [paramsDics setObject:@"0" forKey:@"applasttime"];
        [paramsDics setObject:@1 forKey:@"newapp"];
    }
    User_member *cur_member = [User_member instance];
    //读memberupdate
    NSString *memberupdate = [NSString stringWithFormat:@"%ld",(long)cur_member.memberupdate];
    //检查是否存在memberupdate
    if (memberupdate != nil) {
        [paramsDics setObject:memberupdate forKey:@"memberupdate"];
    }
    //获取post参数
    NSString *passport = cur_member.passport;
    NSString *supe_uid = [NSString stringWithFormat:@"%ld",(long)cur_member.uid];
    [paramsDics setObject:passport forKey:@"passport"];
    [paramsDics setObject:supe_uid forKey:@"supe_uid"];
    NSString *urlString = [NSString stringWithFormat:@"%@%@&appid=%@&applasttime=%@&appversion=%@&memberupdate=%@&mob=%@&passport=%@&supe_uid=%@",baseurl_url,url,paramsDics[@"appid"],paramsDics[@"applasttime"],paramsDics[@"appversion"],paramsDics[@"memberupdate"],paramsDics[@"mob"],paramsDics[@"passport"],paramsDics[@"supe_uid"]];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *operation = [manager  POST:urlString  parameters:parameters  constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        NSData * data =  UIImageJPEGRepresentation(image, 0.5); //将拍下的图片转化成data
        
        [formData appendPartWithFileData:data name:imageAddress fileName:@"121.jpeg" mimeType:@"jpg/jpeg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject){
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:responseObject];
        [dic setObject:image forKey:@"image"];
        _com1(dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        _com1([NSDictionary dictionaryWithObjectsAndKeys:image,@"image", nil]);
        NSLog(@"Error: %@", error);
        
    }];
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {
        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
        CGFloat a = totalBytesWritten;
        CGFloat b = totalBytesExpectedToWrite;
        NSNumber *x =  [NSNumber numberWithFloat:a/b];
        _com2(x);
    }];
    [operation start];
}

@end
